/*
 * Copyright (c) 2018-2999 湖南洛云网络科技有限公司 All rights reserved.
 *
 * https://www.gz-yami.com/
 *
 * 未经允许，不可做商业用途！
 *
 * 版权所有，侵权必究！
 */

package com.yami.shop.delivery.api.listener;

import com.yami.shop.bean.enums.DeliveryType;
import com.yami.shop.bean.event.CalculateTransfeeEvent;
import com.yami.shop.delivery.comment.service.SameCityService;
import com.yami.shop.delivery.comment.service.TransportManagerService;
import lombok.AllArgsConstructor;
import org.springframework.context.event.EventListener;
import org.springframework.stereotype.Component;

import java.util.Collections;
import java.util.Objects;

/**
 * 计算运费的监听事件
 *
 * @author lhd
 */
@Component("calculateTransFeeListener")
@AllArgsConstructor
public class CalculateTransFeeListener {

    private final TransportManagerService transportManagerService;
    private final SameCityService sameCityService;

    /**
     * 计算运费的监听事件
     */
    @EventListener(CalculateTransfeeEvent.class)
    public void calculateTransFeeEvent(CalculateTransfeeEvent event) {
        double transfee = 0.0;
        if(Objects.equals(event.getDeliveryType(), DeliveryType.SAME_CITY.getValue())) {
            transfee = sameCityService.calculateTransFee(Collections.singletonList(event.getProductItem()), event.getUserAddr());
        }else{
            transfee = transportManagerService.calculateTransfee(event.getProductItem(), event.getUserAddr());
        }
        event.setTransfee(transfee);
    }

}
